﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineJobPortal.Models;
using System.Web.DataAccess;
using System.Web.Security;
using System.Web.Routing;
using System.Data;
namespace JobPortal.Controllers
{
    public class HomeController : Controller
    {
        JobPortalEntities7 objEntities = new JobPortalEntities7();
        private IHomeRepository home;

        public HomeController()
        {
        }
        public HomeController(IHomeRepository h)
        {
            home = h;
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult VerifyUser(User user)
        {
            User u = objEntities.Users.First(x => x.Email == user.Email);
            Session.Add("email", user.Email);
            Session.Add("userid", u.UserId);
            Session.Add("name", u.Name);
            // using Repository Pattern
                string email = user.Email;
                string paswrd = user.Password;
                bool check = home.VerifyUser(email, paswrd);
                if (check == true && user.Type.Equals("User"))
                    return RedirectToAction("Home", "User");
              
                if (check == true && user.Type.Equals("Company"))
                    return View("~/Views/Job/Home.cshtml");
            
             return View("Index");
           
        }
        public JsonResult Search(string search)
        {
           var data = objEntities.Jobs.Where(j => j.JobName.Contains(search)).ToList();
           //var data = from j in objEntities.Jobs
           //           where j.JobName.Contains(search)
           //           select j;
            var mydata= data.ToList();
             object[] array = new Job[mydata.Count()];
             int i = 0;
             foreach(var job in data)
             {
                 Job j = job;
                 array[i] = (object)j;
                 i++;
             }
             string[] arr = { "rabia", "mubushra" };
             return this.Json(new { list = data.ToList()}, JsonRequestBehavior.AllowGet);
                
        }
        public ActionResult Search1()
        {
            object email = Session["email"];
            if (email != null)
            {
                string text = Request["search"];
                var data = objEntities.Jobs.Where(j => j.JobName.Contains(text)).ToList();
                ViewBag.x = data;
                if (data.Count != 0)
                {
                    return View("ViewAll");
                }
                return View("NoSearch");
            }

            return RedirectToAction("Index", "Home");

        }
        public ActionResult AboutUs()
        {
            return View();
        }
        public ActionResult Categories()
        {
            object value = Session["email"];
            if (value != null)
            {
                ViewBag.email = value;
                ViewBag.userid = Session["userid"];
                return View();
            }
            return RedirectToAction("Index", "Home");
        }
        public ActionResult ContactUs()
        {
            return View();
        }
        public ActionResult TopJobs()
        {
            return View();
        }
        public ActionResult Gallery()
        {
             object value = Session["email"];
             if (value != null)
             {
                 ViewBag.X = objEntities.Galleries.ToList();
                 return View();
             }
             return RedirectToAction("Index", "Home");
        }
    }
}
